/*
Copyright (c) 2009, Dan Hagerstrand and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cirrus Creative nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BENG_INTERFACE_H
#define BENG_INTERFACE_H

#define BENG_END_OF_MAIN END_OF_MAIN()

#define BENG_INTERFACE_UNLOADED 0
#define BENG_INTERFACE_LOADED_TEXT 1
#define BENG_INTERFACE_LOADED_VIDEO 2

#define BENG_INTERFACE_TIMER_PROCESS 0
#define BENG_INTERFACE_TIMER_DRAW 1

#include <bitset>

class BengSystem;

class BengInterface {
    friend class BengSystem;
    friend class BengIGfx;
    friend class BengISfx;

    private:

    volatile int ticQueue_; // Accumulates for each tic, then empties into ticsCurrent_
    volatile int tics_; // Lifetime number of tics
    int ticsCurrent_; // Counts down number of tics left to process in the main loop

    volatile int fpsTics_; // Holds FPS information
    volatile int fpsLast_; // Last second's FPS information
    volatile int fpsTotalTics_; // Total tics since last newline
    volatile int fpsIndex_; // Number of FPS values since last average calculation
    volatile char fpsUpdate_; // Set to non-zero when ready to update.

    void updateName_(void);
    void displayError_(int severity, char *errorstring);

    protected:

    char guiMode; // Needed for Allegro to display errors in popups.
    bool isInit; // Allows deinit.

    BengSystem *system;

    struct BengTextIdMap { // TODO: Replace with something better.
        int id;
        const char *text;
    };

    static void incrementTics_(void *param); // Standard game timer
    static void incrementFpsTics_(void *param); // FPS timer set to 1 second

    public:

    BengIGfx *iGfx;
    BengISfx *iSfx;

    BengInterface(BengSystem *system)
    {
        this->iGfx = new BengIGfx(this);
        this->iSfx = new BengISfx(this);
        this->system = system;

        this->guiMode = BENG_INTERFACE_UNLOADED;
        this->isInit = false;
        this-> ticQueue_ = this-> ticsCurrent_ = this-> tics_
        = this->fpsIndex_ = this->fpsLast_ = this->fpsTics_ = this->fpsTotalTics_
        = this->fpsUpdate_ = 0;
    }
    ~BengInterface(void);

    int init(void);
    void deinit(void);

    void startLoop(std::bitset<2> &result);

    int getTics(void);

};

#endif
